var details = {};
var goodsId;  //商品ID
var shopId;
var time;
var dateLong;
var peopleId
var num; //商品数量
$(function() {
   //获取url中的参数
	var loc = window.location.search.substr(1);
	var arr = []

	loc = loc.split('&');
	for(var i = 0; i < loc.length; i++) {
		var theRequest = new Object();
		theRequest.title = loc[i].split("=")[0];
		theRequest.value = loc[i].split("=")[1];
		arr.push(theRequest)
	}

	console.log(arr)
	goodsId = arr[4].value;
	shopId = arr[3].value;
	time = arr[1].value;
	dateLong = arr[0].value;
	peopleId = arr[2].value;
	num = arr[5].value;

	var to = new Date(parseInt(dateLong)).toLocaleString().replace(/:\d{1,2}$/, ' ');
	to = to.split(' ');

	details.shuliang = num;
	details.servTime = to[0] + ' ' + time;

	console.log(details)
	gethotkey()
	getGoods();
	getStaff();
	getyhq();
	count();
	getData();
    getMoney();
})
//获取要支付金额
function getMoney(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/orderDetail/calculateMoney",
		async:false,
		data:{
			goodsId:goodsId,
			buyCount:num,
			ptCoupons:details.num,//平台抵扣券数量
			userCouponsDiscountId:details.yhqID
		},
		success:function(res){
			console.log(res.data);
			details.payMoney = res.data.realPay;
			getData();
		}
	});

}
//提交订单
function submit() {
		//联系电话
       	var tel = document.querySelector('.myTel').value;
       	//到店时间
       	var val = document.querySelector('select').value
       	console.log(tel,val)
		console.log(details)
		 $.ajax({
		 	type:"get",
			url:baseUrl+"hsent-xxyl-user/orderDetail/addOrderDetailService",
			async:false,
			data:{
				    shopId:shopId,//商家ID
				    goodsId:goodsId,//产品服务ID
	                buyCount:num,//产品服务购买数量
	                userCouponsDiscountId:details.yhqID,//商家优惠券抵扣编号  one
	                ptCouponsDiscount:details.num*10,//平台消费抵扣券抵扣金额 two
	                orderAmount:details.shuliang*details.goods.hsPrice,//订单金额 three
	                payAmount:details.payMoney,//实付金额  four
	                contactName:'',//预约联系人名称  five
	                contactPhone:tel,//预约联系人电话 six
	                applyServiceDate:dateLong,//预约服务日期时间戳
	                applyServiceTime:time,//预约服务时间段  格式:8:00-10:100
	                applyServiceStaffsIds:peopleId,//预约服务人员集合字符串  例如:12,13,15
	                applyServiceAddress:'',//预约上门服务地址 seven
	                userNotes:'',//用户下单备注
	                exveptShopTime:-1,
					pc:1
			},
			success:function(res){
				console.log('跳转支付')
				$('#cover1').remove()
				$('#payBody').html(res);
			   // console.log(res);
			}
		 })
}

function  getGoods(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/service/tGoodsUserBasicInfo",
		async:false,
		data:{
			id:goodsId,
			shopId:shopId
		},
		success:function(res){
			// console.log(JSON.stringify(res))
			details.goods = res.data;
			return details;
		}
	});
	return details;
}
function getStaff(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/staffs/selectStaffs",
		async:false,
		data:{
			staffId:peopleId
		},
		success:function(res){

			details.staff = res.data;
			return details;
		}
	});
	return details;
}
function getyhq(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/coupon/getCouponGot",
		async:false,
		data:{
			shopId:shopId,
			goodsId:goodsId,
			count:num
		},
		success:function(res){
			console.log(res.data.available)
			 details.yhq = res.data.available;
			 return details;
		}
	});
	return details;
}
//增加抵扣券
function addNum() {
	if(details.num < 5) {
		details.num++;
		 getMoney()
	}
		return ;


}
//减少抵扣券
function minusNum() {
	console.log(details.num);
	if(details.num > 0 ) {
		details.num--;
		 getMoney()
	}
   return;
}
function count(){
	$.ajax({
		type:"get",
		url:baseUrl+"hsent-xxyl-user/consumer/getCanUseVoucherNum",
		async:false,
		data:{
			goodsId:goodsId,
			count:num
		},
		success:function(res){
			console.log(res)
		    details.num = res.data.num;
		    return details;
		}
	})
	return details;
}

//模板渲染
function getData() {
	console.log(details)
	orderCon = template('test', details);
	document.getElementById('goods').innerHTML = orderCon;
}

//选择优惠券
function selectedYhq(list) {
	console.log(list);
	details.selected = list.couponAmount;
	details.yhqID = list.id;
	getMoney()
}



